package com.linyu.huawei.simple;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 相同数字的积木游戏
 *
 * 哈希表解法
 */
public class SameNumberBlockGame {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 存储每个数字第一次出现的位置
        HashMap<Integer, Integer> firstAppearMap = new HashMap<>();
        // 存储每个数字的最大距离
        HashMap<Integer, Integer> maxPointMap = new HashMap<>();

        System.out.print("请输入积木总数：");
        int count = sc.nextInt();

        for (int i = 1; i <= count; i++) {
            int nowNum = sc.nextInt();

            if (!firstAppearMap.containsKey(nowNum)){
                firstAppearMap.put(nowNum, i);
            }else {
                maxPointMap.put(nowNum, i - firstAppearMap.get(nowNum));
            }
        }
        if (maxPointMap.isEmpty()){
            System.out.println("-1");
            return;
        }
        Map.Entry<Integer, Integer> integerIntegerEntry = maxPointMap.entrySet().stream().max((m1, m2) -> m1.getValue() - m2.getValue()).get();
        System.out.println(integerIntegerEntry.getValue());

    }
}
